package com.byd.modules.biz.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byd.common.exception.RRException;
import com.byd.modules.biz.dao.EmotionDao;
import com.byd.modules.biz.dao.TbFlagDao;
import com.byd.modules.biz.dao.UserDictDao;
import com.byd.modules.biz.entity.DimensionEntity;
import com.byd.modules.biz.entity.EmotionEntity;
import com.byd.modules.biz.service.EmotionService;
import com.byd.modules.biz.service.ExcelService;
import com.byd.modules.biz.service.UserDictService;



@Service("emotionService")
public class EmotionServiceImpl implements EmotionService {
	@Autowired
	private EmotionDao emotionDao;
	
	@Autowired
    private ExcelService excelService;
	
	@Autowired
    private TbFlagDao tbFlagDao;
	
//	@Autowired
//    private UserDictDao userDictDao;
	@Autowired
	private UserDictService userDictService;
	
	@Override
	public EmotionEntity queryObject(Long id){
		return emotionDao.queryObject(id);
	}
	
	@Override
	public List<EmotionEntity> queryList(Map<String, Object> map){
		return emotionDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return emotionDao.queryTotal(map);
	}
	
	@Override
    @Transactional
	public void save(EmotionEntity emotion){
		EmotionEntity parentEnt = this.emotionDao.queryObject(emotion.getParentId());
		try {
			emotionDao.save(emotion);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                throw new RRException("已存在这个情感词！");
            } else {
                throw new RRException("情感词保存失败！");
            }
        }
		emotion.setLinkId(parentEnt!=null?parentEnt.getLinkId()+emotion.getId()+"_":"_"+emotion.getId()+"_");
		emotion.setLinkName(parentEnt!=null?parentEnt.getLinkName()+emotion.getEmotionWord()+"_":"_"+emotion.getEmotionWord()+"_");
		emotion.setLevel(parentEnt!=null?parentEnt.getLevel()+1:1);
		emotion.setParentName(parentEnt!=null?parentEnt.getEmotionWord():"");
		if (emotion.getDimensionId()==null || emotion.getDimensionId()==-1 || emotion.getDimensionId()==-0) {
			emotion.setDimensionId((long) 0);
			emotion.setDimensionName("");
		}
		emotionDao.update(emotion);
		
		//userDictDao.saveWord(emotion.getEmotionWord());
		userDictService.saveName(emotion.getEmotionWord());
		tbFlagDao.updateCount(2);
	}
	
	@Override
    @Transactional
	public void update(EmotionEntity emotion){
		EmotionEntity parentEnt = this.emotionDao.queryObject(emotion.getParentId());
		emotion.setLinkId(parentEnt!=null?parentEnt.getLinkId()+emotion.getId()+"_":"_"+emotion.getId()+"_");
		emotion.setLinkName(parentEnt!=null?parentEnt.getLinkName()+emotion.getEmotionWord()+"_":"_"+emotion.getEmotionWord()+"_");
		emotion.setLevel(parentEnt!=null?parentEnt.getLevel()+1:1);
		emotion.setParentName(parentEnt!=null?parentEnt.getEmotionWord():"");
		if (emotion.getDimensionId()==null || emotion.getDimensionId()==-1 || emotion.getDimensionId()==-0) {
			emotion.setDimensionId((long) 0);
			emotion.setDimensionName("");
		}
		try {
			emotionDao.update(emotion);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                throw new RRException("已存在这个情感词！");
            } else {
                throw new RRException("情感词保存失败！");
            }
        }

		//userDictDao.saveWord(emotion.getEmotionWord());
		userDictService.saveName(emotion.getEmotionWord());
		tbFlagDao.updateCount(2);
	}
	
	@Override
	public void delete(Long id){
		emotionDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		emotionDao.deleteBatch(ids);
	}
	
	 /**
     * 重载方法
     * @param parentId
     * @param response
     */
    @Override
    public void export(Map<String, Object> query,HttpServletResponse response) {
        List<EmotionEntity> queryList = emotionDao.queryListTree(query);
		for (EmotionEntity emotion : queryList) {
			emotion.setEmotionWord(dim2Space(emotion.getLevel(), emotion.getEmotionWord()));
		}
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=emotion.xls");
        excelService.exportExcel(new ExportParams(), EmotionEntity.class, queryList, response);
    }

    public String dim2Space(int level,String name){
        String newName="";
        for (int i = 1; i < level; i++) {
            newName+="  ";
        }
       return newName+name; 
    }

	@Override
	public List<EmotionEntity> queryListFilter(Long parentId) {
        return emotionDao.queryListFilter(parentId);
	}

	@Override
	public List<EmotionEntity> queryListFilterByName(String name) {
        return emotionDao.queryListFilterByName(name);
	}
}
